﻿using System;
using System.Windows.Data;

namespace LiveTiss.Classes.TypeConverter
{
    public class StringConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is string && parameter is string)
            {
                string key = value as string;
                var list = XML_enum.GetEnum(parameter.ToString());

                int index = -1;
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Key.ToLower().Equals(key.ToLower()))
                    {
                        index = i;
                        break;
                    }
                }

                return index;
            }
            else
            {
                return -1;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is int && parameter is string)
            {
                var index = (int)value;
                if (index >= 0)
                {
                    var list = XML_enum.GetEnum(parameter.ToString());

                    return list[index].Key;
                }
            }
            return null;
        }

        #endregion
    }
}
